package cn.tangwei.pay.service.impl;

import cn.tangwei.basic.exception.BusinessException;
import cn.tangwei.basic.service.impl.BaseServiceImpl;
import cn.tangwei.order.service.IProductOrderService;
import cn.tangwei.pay.domain.AlipayInfo;
import cn.tangwei.pay.domain.PayBill;
import cn.tangwei.pay.mapper.AlipayInfoMapper;
import cn.tangwei.pay.mapper.PayBillMapper;
import cn.tangwei.pay.service.IPayBillService;
import cn.tangwei.pay.utils.AlipayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PayBillServiceImpl extends BaseServiceImpl<PayBill> implements IPayBillService {

    @Autowired
    private AlipayInfoMapper alipayInfoMapper;

    @Autowired
    private PayBillMapper payBillMapper;

    @Autowired
    private IProductOrderService productOrderService;

    @Override
    public String pay(PayBill bill) {
        // 1.空值校验
        if (bill == null) {
            throw new BusinessException("支付数据填写为空");
        }
        // 查询支付订单
        PayBill payBill = payBillMapper.queryById(bill.getId());
        if (payBill == null) {
            throw new BusinessException("支付数据填写为空");
        }
        // 区分支付类型 //0余额 1支付宝 2微信 3银联
        switch (payBill.getPayChannel().intValue()) {
            case 1: // 支付宝支付
                AlipayInfo info = alipayInfoMapper.loadByShopId(payBill.getShop_id());
                return AlipayUtils.pay(info, bill);
            case 2 ://微信
                //语句
                break; //可选
            case 3 ://银联
                //语句
                break; //可选
            default : //余额
                //语句
        }
        return null;
    }

    @Override
    public PayBill loadByUnionPaySn(String unionPaySn) {
        return payBillMapper.loadByUnionPaySn(unionPaySn);
    }
}
